﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

//订单服务，实现对订单查询，修改等功能
namespace Homework8_Order
{
    public class OrderService
    {
        public List<Order> OrderList { get; set; } = new List<Order>();
        System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(List<Order>));
        //添加订单
        public void addOrder(Order a)
        {
            bool flag = true;
            foreach (Order order in OrderList)
            {
                //订单号重复
                if (a.Equals(order))
                {
                    Console.WriteLine("订单号已存在，无法再次添加");
                    flag = false;
                    break;
                }

            }
            //如果未重复添加，则加入该订单
            if (flag == true)
            {
                OrderList.Add(a);
                Console.WriteLine("添加成功！");
            }
        }
        //查询订单,先写依据订单号的查询
        public Order searchOrder(int ID)
        {
            var order = from w in OrderList where w.OrderID == ID select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("未查询到该订单，请检查你输入的订单号是否正确。");
                return null;
            }
            order.FirstOrDefault().ToString();
            return order.FirstOrDefault();
        }
        public Order searchOrder(string name)
        {
            var order = from w in OrderList where w.Name == name select w;
            if (order.FirstOrDefault() == null)
            {
                Console.WriteLine("未查询到该用户，请检查你输入的用户名是否正确。");
                return null;
            }
            order.FirstOrDefault().ToString();
            return order.FirstOrDefault();
        }
        //删除订单
        public void deleteOrder(int ID)
        {
            var order = from w in OrderList where w.OrderID == ID select w;
            if (order.FirstOrDefault() != null)
            {
                OrderList.Remove(order.FirstOrDefault());
                Console.WriteLine("该订单删除成功");
            }
            else
                Console.WriteLine("该订单不存在");
        }

        //修改订单
        public void AlterOrder(int ID)
        {
            var order = from w in OrderList where w.OrderID == ID select w;
        }
        //导出订单
        public void Export(String path)
        {

            using (FileStream F = new FileStream(path, FileMode.OpenOrCreate))
            {
                xml.Serialize(F, OrderList);
                F.Close();
            }
            Console.WriteLine("导出成功.");
        }
        //导入订单
        public void Import(String path)
        {
            using (FileStream F = new FileStream(path, FileMode.Open))
            {
                List<Order> order = (List<Order>)xml.Deserialize(F);
                F.Close();
            }
            Console.WriteLine("导入成功.");
        }
    }
}

